package com.wang.system.controller;

import com.wang.common.core.controller.BaseController;
import com.wang.common.core.domain.R;
import com.wang.common.core.domain.TableDataInfo;
import com.wang.common.core.enums.ResultCode;
import com.wang.system.domain.question.dto.QuestionAddDTO;
import com.wang.system.domain.question.dto.QuestionEditDTO;
import com.wang.system.domain.question.dto.QuestionQueryDTO;
import com.wang.system.domain.question.vo.QuestionDetailVO;
import com.wang.system.domain.question.vo.QuestionVO;
import com.wang.system.service.IQuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "题目管理接口")
@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {
    @Autowired
    private IQuestionService questionService;

    @Operation(summary = "获取题目列表")
    @GetMapping("/list")
    public TableDataInfo list(QuestionQueryDTO questionQueryDTO){
        List<QuestionVO> list = questionService.list(questionQueryDTO);
        return BaseController.getTableDataInfo(list);
    }

    @Operation(summary = "添加题目")
    @PostMapping("/add")
    public R<Void> add(@RequestBody @Validated QuestionAddDTO questionAddDTO){
        return BaseController.toR(questionService.add(questionAddDTO));
    }

    @Operation(summary = "获取题目详细信息")
    @GetMapping("/detail")
    public R<QuestionDetailVO> detail(Long questionId){
        QuestionDetailVO questionDetailVO = questionService.detail(questionId);
        return questionDetailVO == null ? R.fail(ResultCode.FAILED_RESOURCE_NOT_EXISTS) : R.success(questionDetailVO);
    }

    @Operation(summary = "编辑题目")
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody @Validated QuestionEditDTO questionEditDTO){
        return BaseController.toR(questionService.edit(questionEditDTO));
    }

    @Operation(summary = "删除指定题目")
    @DeleteMapping("/delete")
    public R<Void> delete(Long questionId){
        return BaseController.toR(questionService.delete(questionId));
    }
}
